<script lang="ts" setup>
import { ref } from 'vue'

const inputJson = ref('')
const outputJson = ref('')
const errorMessage = ref('')
const isValid = ref(true)

function formatJson() {
  try {
    const parsed = JSON.parse(inputJson.value)
    outputJson.value = JSON.stringify(parsed, null, 2)
    errorMessage.value = ''
    isValid.value = true
  } catch (error) {
    errorMessage.value = `JSON格式错误: ${(error as Error).message}`
    isValid.value = false
    outputJson.value = ''
  }
}

function compressJson() {
  try {
    const parsed = JSON.parse(inputJson.value)
    outputJson.value = JSON.stringify(parsed)
    errorMessage.value = ''
    isValid.value = true
  } catch (error) {
    errorMessage.value = `JSON格式错误: ${(error as Error).message}`
    isValid.value = false
    outputJson.value = ''
  }
}

function validateJson() {
  try {
    JSON.parse(inputJson.value)
    errorMessage.value = 'JSON格式正确 ✓'
    isValid.value = true
  } catch (error) {
    errorMessage.value = `JSON格式错误: ${(error as Error).message}`
    isValid.value = false
  }
}

function clearAll() {
  inputJson.value = ''
  outputJson.value = ''
  errorMessage.value = ''
  isValid.value = true
}

function copyToClipboard() {
  if (outputJson.value) {
    navigator.clipboard.writeText(outputJson.value).then(() => {
      errorMessage.value = '已复制到剪贴板 ✓'
      isValid.value = true
    })
  }
}

function loadSample() {
  inputJson.value = `{
  "name": "张三",
  "age": 30,
  "city": "北京",
  "hobbies": ["读书", "游泳", "编程"],
  "address": {
    "street": "长安街1号",
    "zipcode": "100000"
  },
  "isActive": true
}`
}
</script>

<template>
  <div class="json-formatter">
    <div class="controls">
      <button class="btn btn-primary" @click="formatJson">格式化</button>
      <button class="btn btn-secondary" @click="compressJson">压缩</button>
      <button class="btn btn-info" @click="validateJson">验证</button>
      <button class="btn btn-success" @click="copyToClipboard" :disabled="!outputJson">复制结果</button>
      <button class="btn btn-warning" @click="loadSample">加载示例</button>
      <button class="btn btn-danger" @click="clearAll">清空</button>
    </div>

    <div class="status" v-if="errorMessage" :class="{ 'error': !isValid, 'success': isValid }">
      {{ errorMessage }}
    </div>

    <div class="editor-container">
      <div class="editor-section">
        <h3>输入JSON</h3>
        <textarea 
          v-model="inputJson" 
          class="json-input"
          placeholder="请输入JSON数据..."
          spellcheck="false"
        ></textarea>
      </div>

      <div class="editor-section">
        <h3>输出结果</h3>
        <textarea 
          v-model="outputJson" 
          class="json-output"
          placeholder="格式化后的JSON将显示在这里..."
          readonly
          spellcheck="false"
        ></textarea>
      </div>
    </div>
  </div>
</template>

<style scoped>
.json-formatter {
  width: 100%;
}

.controls {
  display: flex;
  gap: 10px;
  margin-bottom: 20px;
  flex-wrap: wrap;
}

.btn {
  padding: 8px 16px;
  border: none;
  border-radius: 6px;
  cursor: pointer;
  font-size: 14px;
  font-weight: 500;
  transition: all 0.2s ease;
  color: #fff;
}

.btn:disabled {
  opacity: 0.5;
  cursor: not-allowed;
}

.btn-primary {
  background: #007bff;
}

.btn-primary:hover:not(:disabled) {
  background: #0056b3;
}

.btn-secondary {
  background: #6c757d;
}

.btn-secondary:hover:not(:disabled) {
  background: #545b62;
}

.btn-info {
  background: #17a2b8;
}

.btn-info:hover:not(:disabled) {
  background: #117a8b;
}

.btn-success {
  background: #28a745;
}

.btn-success:hover:not(:disabled) {
  background: #1e7e34;
}

.btn-warning {
  background: #ffc107;
  color: #212529;
}

.btn-warning:hover:not(:disabled) {
  background: #e0a800;
}

.btn-danger {
  background: #dc3545;
}

.btn-danger:hover:not(:disabled) {
  background: #c82333;
}

.status {
  padding: 12px;
  border-radius: 6px;
  margin-bottom: 20px;
  font-weight: 500;
}

.status.error {
  background: rgba(220, 53, 69, 0.1);
  color: #dc3545;
  border: 1px solid rgba(220, 53, 69, 0.3);
}

.status.success {
  background: rgba(40, 167, 69, 0.1);
  color: #28a745;
  border: 1px solid rgba(40, 167, 69, 0.3);
}

.editor-container {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 20px;
  height: 500px;
}

.editor-section {
  display: flex;
  flex-direction: column;
}

.editor-section h3 {
  margin: 0 0 10px 0;
  color: #fff;
  font-size: 1.1em;
}

textarea {
  flex: 1;
  background: rgba(0, 0, 0, 0.3);
  border: 1px solid rgba(255, 255, 255, 0.2);
  border-radius: 6px;
  padding: 15px;
  color: #fff;
  font-family: 'Consolas', 'Monaco', 'Courier New', monospace;
  font-size: 14px;
  line-height: 1.5;
  resize: none;
  outline: none;
}

textarea:focus {
  border-color: rgba(255, 255, 255, 0.4);
}

textarea::placeholder {
  color: rgba(255, 255, 255, 0.5);
}

.json-output {
  background: rgba(0, 0, 0, 0.4);
}

@media (max-width: 768px) {
  .editor-container {
    grid-template-columns: 1fr;
    height: auto;
  }
  
  .editor-section {
    height: 300px;
  }
  
  .controls {
    justify-content: center;
  }
}
</style>